package com.lantu.wisdom.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lantu.framework.common.constants.exceptions.ASException;
import com.lantu.paycommon.enums.OrderStateEnum;
import com.lantu.wisdom.common.enums.BillDetailsTypeEnum;
import com.lantu.wisdom.common.enums.code.ParamResponseCode;
import com.lantu.wisdom.common.utils.LogUtil;
import com.lantu.wisdom.dto.PlaceOrderDTO;
import com.lantu.wisdom.mapper.PayDetailsMapper;
import com.lantu.wisdom.mapper.WUserMapper;
import com.lantu.wisdom.model.PayDetail;
import com.lantu.wisdom.model.WUser;
import com.lantu.wisdom.service.PayDetailApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
@Slf4j
@Service
public class PaydetailServiceImpl implements PayDetailApi {

    @Autowired
    private PayDetailsMapper userBillDetailsMapper;

    @Autowired
    private WUserMapper userInfoMapper;


    @Override
    public boolean addActivityUserBill(PlaceOrderDTO dto, String orderCode) throws ASException {
        WUser userInfo = userInfoMapper.selectOne(new LambdaQueryWrapper<WUser>().eq(WUser::getOpenId,dto.getOpenId()));
        if (null == userInfo) {
            log.error(LogUtil.CONTENT, "openId erro");
            throw new ASException(ParamResponseCode.OPENID_ERRO);
        }
        Long time = System.currentTimeMillis();
        PayDetail userBillDetails = new PayDetail();
        userBillDetails.setUserCode(userInfo.getUserId());
        userBillDetails.setType(BillDetailsTypeEnum.INCOME.getType());
        userBillDetails.setDetailedType(BillDetailsTypeEnum.INCOME_PAY.getType());
        userBillDetails.setOrderCode(orderCode);
//        userBillDetails.setActivityCode(dto.getActivityCode());
        userBillDetails.setState(1);
        userBillDetails.setMoney(dto.getTotal());
        userBillDetails.setCreateTime(time);
        userBillDetails.setUpdateTime(time);
        return 1 == userBillDetailsMapper.insert(userBillDetails);
    }

    @Transactional
    @Override
    public boolean Pay(String orderCode,BigDecimal money) throws ASException {

        PayDetail details = userBillDetailsMapper.selectOne(new LambdaQueryWrapper<PayDetail>().eq(PayDetail::getOrderCode,orderCode));
        if(null != details&&details.getState()== OrderStateEnum.WAIT_BAY.getState()) {
            details.setMoney(money);
            log.info(LogUtil.CONTENT, "订单 ：" + orderCode + "已经支付,修改订单状态");
            details.setState(OrderStateEnum.BAY.getState());
            details.setUpdateTime(System.currentTimeMillis());
            userBillDetailsMapper.updateById(details);
           // rechargeSuccess(orderCode);
        }else {
            log.info(LogUtil.CONTENT,"没有此订单 orderCode="+orderCode);
        }
        return true;
    }

    @Override
    public boolean isPay(String orderCode) throws ASException {
        PayDetail details = userBillDetailsMapper.selectOne(new LambdaQueryWrapper<PayDetail>().eq(PayDetail::getOrderCode,orderCode));
        if(null != details&&details.getState()== OrderStateEnum.BAY.getState()){
            return true;
        }
        return false;
    }


}
